<?php

namespace App\Admin\Controller\Rbac;

class Role extends \App\Admin\Controller\Authed {
    
    public function index() {
        $rows = QF()->Rbac()->Role()->getTreeArray();
        foreach ($rows as &$row) {
            $row['state_title'] = QF()->Rbac()->Role()->getStateAssoc($row['state']);
        }
        unset($row);
        $this->assign('rows', $rows);
    }
    
    public function edit() {
        
        $id = intval($this->request->get('id'));
        
        if ($this->request->isPost()) {
            
            $data = $this->request->get();
            
            if ($id) {
                $result = QF()->Rbac()->Role()->update($id, $data);
            } else {
                $result = QF()->Rbac()->Role()->add($data);
            }
            
            if ($result) {
                succ('', '', 2);
            } else {
                fail('修改失败');
            }
        }
        
        $detail = QF()->Rbac()->Role()->get($id);
        
        $this->assign('detail', $detail);
        
        $this->assign('parent_routes', QF()->Rbac()->Role()->getTreeArray());
        
        // 获取角色被授权的路由ID
        $route_ids = QF()->Rbac()->Role()->getRouteIds($id);
        
        // 获取管理员列表
        $this->routes = QF()->Rbac()->Route()->getCheckedTree($route_ids);
    }
    
    public function remove() {
        $id = intval($this->request->get('id'));
        $result = QF()->Rbac()->Role()->remove($id);
        if ($result) {
            succ();
        } else {
            fail();
        }
    }
    
}